package com.sqly.doc.menu.grjbxx;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.GrxxFh;
import com.sqly.doc.pojo.JuMin;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.user.choose_user;
import com.sqly.doc.util.CreateBitmap;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.CustomDatePicker;
import com.sqly.doc.view.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import cz.msebera.android.httpclient.Header;

//import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

public class grjbxx_fh extends MyBaseActivity {

    //标题
    TextView title, uname;
    LinearLayout back;
    Context con = grjbxx_fh.this;
    private CustomDatePicker customDatePicker;

    private TextView tbtime;
    private TextView XINGMING;
    private EditText content;


    RadioGroup type;

    GrxxFh obj;
    boolean isadd=false;//是否是新添加
    String url="",JKDAH="";
    ImageView sfysqm, brqm, xcqm;
    String sfysqmpic = "", brqmpic = "";
    JuMin j;

    @Override
    protected void initUI() {
        setContentView(R.layout.grjbxx_fh);

        //BinderUtil.exeFindById(grjbxx_fh.this);
        if (NetWorkUtils.isNetworkConnected(con)) {
            if (getIntent().getStringExtra("type").equals("edit")) {
                obj = (GrxxFh) getIntent().getExtras().getSerializable("b");
                init(obj);
            }else {
                obj = new GrxxFh();
                init(obj);
            }
        }
    }

    private void init(final GrxxFh obj){
        //运行时生成activity中所有控件的findViewById方法
        //BinderUtil.exeFindById(fjhdycrhsf.this);
        //BinderUtil.initData(grjbxx_fh.this, obj);

        //姓名
        XINGMING = (TextView) findViewById(R.id.XINGMING);
        if (obj != null && !StringUtils.isEmpty(obj.getXINGMING())) {
            XINGMING.setText(obj.getXINGMING());
        }
        XINGMING.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!NetWorkUtils.isNetworkConnected(con)) {
                    MyToastUtil.ShowToast(con, "联网失败！");
                }else {
                    Bundle bundle = getIntent().getExtras();
                    if (null != bundle.getString("type") && "add".equals(bundle.getString("type"))) {
                        startActivityForResult(new Intent(con, choose_user.class).putExtra("cname", "zyy_tzbs.class"), 1);
                    }
                }

            }
        });
        //填表日期
        tbtime = (TextView) findViewById(R.id.tbtime);
        if(obj != null && !StringUtils.isEmpty(obj.getFHRQ())){
            tbtime.setText(DateConvert.dateToString(DateConvert.toDate(obj.getFHRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
        }
        tbtime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        tbtime.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        initRadioButton();

        back = (LinearLayout) findViewById(R.id.back);
        content = (EditText) findViewById(R.id.content);
        if(obj != null && !StringUtils.isEmpty(obj.getFHNR())){
            content.setText(obj.getFHNR());
        }
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("个人基本信息复核");
        uname.setText("");
//        uname.setText("保存");
//        uname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //boolean validate = BinderUtil.copyDataToView(grjbxx_fh.this, obj);
//                String xm = XINGMING.getText().toString();
//                String cont = content.getText().toString();
//
//                String tbtim = tbtime.getText().toString();
//                if(null == xm || "".equals(xm) || "点我选择".equals(xm)){
//                    MyToastUtil.ShowToast(con,"请选择个人信息");
//                }else if(null == cont || "".equals(cont)){
//                    MyToastUtil.ShowToast(con,"请输入复核内容");
//                }else if(null == tbtim || "".equals(tbtim)){
//                    MyToastUtil.ShowToast(con,"请选择复核日期");
//                }else if (NetWorkUtils.isNetworkConnected(con)) {  ///网络连接
//                    //RequestParams ps = BinderUtil.generateRequestParamsByView(grjbxx_fh.this);
//                    RequestParams ps = new RequestParams();
//                    if (isadd) {
//                        ps.add("entity.JKDAH", JKDAH);
//                        url = HttpUrl.SaveXxfh;
//                    } else {
//                        ps.add("entity.id", obj.getId());
//                        ps.add("entity.JKDAH", obj.getJKDAH());
//                        url = HttpUrl.EditXxfh;
//                    }
//                    ps.add("entity.XINGMING", xm);
//                    ps.add("entity.content", cont);
//                    ps.add("entity.tbtime", tbtim);
//
//                    ps.add("entity.type", obj.getType());
//                    ps.add("entity.XINGMING", obj.getXINGMING());
//                    ps.add("entity.tbtime", obj.getFHRQ());
//                    ps.add("entity.CARDNUM", obj.getCARDNUM());
//                    ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
//                    ps.add("type", "2");
//
//                    UserClient.get(url, ps, new AsyncHttpResponseHandler() {
//                        @Override
//                        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
//                            String content = new String(response);
//                            RetMsg ret = JSON.parseObject(content, RetMsg.class);
//                            if (ret.getCode().equals("200")) {
//                                MyToastUtil.ShowToast(con, "成功");
//                            }else{
//                                MyToastUtil.ShowToast(con, ret.getInfo());
//                            }
//                            finish();
//                            //Bundle b = new Bundle();
//                            //b.putSerializable("b", obj);
//                            //startActivity(new Intent(con, zyy_jkglfw.class).putExtras(b).putExtra("type", "edit"));
//                        }
//
//                        @Override
//                        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
//
//                            MyToastUtil.ShowToast(con, "失败");
//                        }
//                    });
//                } else {
//
//                }
//            }
//        });

    }

    private void initRadioButton(){
//        //问题1
//        type = findViewById(R.id.type);
//        if (obj != null && !StringUtils.isEmpty(obj.getType())) {
//            switch (Integer.parseInt(obj.getType())) {
//                case 1:
//                    type.check(R.id.type1);
//                    break;
//                case 2:
//                    type.check(R.id.type2);
//                    break;
//                case 3:
//                    type.check(R.id.type3);
//                    break;
//            }
//        }else{
//            obj.setType("1");
//        }
//        type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                if (checkedId == R.id.type1) {
//                    obj.setType("1");
//                } else if (checkedId == R.id.type2) {
//                    obj.setType("2");
//                }else if (checkedId == R.id.type3) {
//                    obj.setType("3");
//                }
//            }
//        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {

        } else {
            if (requestCode == 1) {
                j = (JuMin) data.getExtras().getSerializable("j");
                getObject();
            }
            if (requestCode == 8) {

                byte[] bis = data.getByteArrayExtra("bitmap");
                Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
                Bitmap bb = CreateBitmap.zoomImg(bmp, 1, 1);
                sfysqm.setImageBitmap(bb);
                sfysqmpic = CreateBitmap.saveMyBitmap(bb);
            }
            if (requestCode == 9) {
                byte[] bis = data.getByteArrayExtra("bitmap");
                Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
                Bitmap bb = CreateBitmap.zoomImg(bmp, 1, 1);
                brqm.setImageBitmap(bb);
                brqmpic = CreateBitmap.saveMyBitmap(bb);
            }
        }
    }

    public void getObject() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("CARDNUM", j.getCARDNUM());
        UserClient.get(HttpUrl.GetXxfhGrjbxx, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    JSONObject j = JSON.parseObject(ret.getRd());
                    obj = new GrxxFh();
                    init(obj);
                    obj.setJKDAH(j.getString("JKDAH"));
                    obj.setXINGMING(j.getString("XINGMING"));
                    obj.setCARDNUM(j.getString("CARDNUM"));
                    JKDAH = j.getString("JKDAH");
                    XINGMING.setText(j.getString("XINGMING"));

                    isadd = true;
                } else {
                    SweetAlertDialogUtil.ErrorDialogNoFinish(con, "错误提示", ret.getInfo(), "确定");
                }

            }
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                isadd = true;
                obj = new GrxxFh();
                init(obj);
            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

}
